<?php

namespace App\Http\Controllers\Frontend\V10\Users;

use App\Events\AuthStepEvent;
use App\Exceptions\GeneralException;
use App\Facades\SamLogFacade;
use App\Http\Requests\Frontend\V10\Users\UserInfoStoreRequest;
use App\Models\UserAuthStepEventLog;
use App\Models\UserInfo;
use App\Models\UserLoanRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Overtrue\LaravelPinyin\Facades\Pinyin;

class UserInfoController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:api');
    }

    /**
     * Description:基础信息录入
     * Author: Sam
     * DateTime: 2019-02-28 13:40
     * @param UserInfoStoreRequest $request
     * @return mixed
     * @throws GeneralException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function store(UserInfoStoreRequest $request)
    {
        $user = $request->user();
        $params = $this->extractInputFromRules($request,$request->rules());
        $params['user_id'] = $user->id;
        $loan = UserLoanRequest::where(['current'=>UserLoanRequest::CURRENT_NOW,'user_id'=>$user->id])->orderByDesc('id')->firstOrFail();
        $info = UserInfo::where('user_id',$user->id)->orderByDesc('created_at')->first();
        if(strlen($params['linkman_first_phone'])>11){
            $params['linkman_first_phone'] = substr($params['linkman_first_phone'],-11);
        }
        if(strlen($params['linkman_second_phone'])>11){
            $params['linkman_second_phone'] = substr($params['linkman_second_phone'],-11);
        }
        DB::beginTransaction();
        try{
            if($info){
                $info->updateLogic($params);
            }else{
                $info = UserInfo::create($params);
            }
            $loan->user_base_info_id = $info->id;
            /**
             * @var $loan UserLoanRequest
             */
            $loan->save();
            $log = [
                'uid'=>$user->id,
                'step_id'=>$info->id,
                'type'=>UserAuthStepEventLog::STEP_INFO,
                'loan_request_id'=>$loan->id,
            ];
            event(new AuthStepEvent($log));
            DB::commit();
        }catch(\Exception $exception){
            SamLogFacade::debug($exception->getMessage());
            DB::rollBack();
            throw new GeneralException($exception->getMessage(),500);
        }
        Cache::delete("user_info_{$user->id}");
        Cache::add("user_info_{$user->id}",json_encode($params),60*24);
        return success([]);
    }

    /**
     * Description:获取用户信息缓存
     * Author: Sam
     * DateTime: 2019-02-27 15:13
     * @param Request $request
     * @return mixed
     */
    public function getInfoCache(Request $request){
        $user = $request->user();
        $cache = Cache::get("user_info_{$user->id}");
        $data = json_decode($cache,true);
        return success($data);
    }
}
